package com.ruoyi.web.controller.teacher;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.domain.BasisUavTable;
import com.ruoyi.service.IBasisUavTableService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 目标的参数Controller
 * 
 * @author wangzebing
 * @date 2025-06-23
 */
@RestController
@RequestMapping("/system/table")
public class BasisUavTableController extends BaseController
{
    @Autowired
    private IBasisUavTableService basisUavTableService;

    @Value("${upload.path}")
    private String uploadFilePath;

    /**
     * 查询目标的参数列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BasisUavTable basisUavTable)
    {
        startPage();
        List<BasisUavTable> list = basisUavTableService.selectBasisUavTableList(basisUavTable);
        return getDataTable(list);
    }

    /**
     * 导出目标的参数列表
     */
    @Log(title = "目标的参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasisUavTable basisUavTable)
    {
        List<BasisUavTable> list = basisUavTableService.selectBasisUavTableList(basisUavTable);
        ExcelUtil<BasisUavTable> util = new ExcelUtil<BasisUavTable>(BasisUavTable.class);
        util.exportExcel(response, list, "目标的参数数据");
    }

    /**
     * 获取目标的参数详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(basisUavTableService.selectBasisUavTableById(id));
    }

    /**
     * 新增目标的参数
     */
    @Log(title = "目标的参数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasisUavTable basisUavTable)
    {

        basisUavTable.setTimeslot(basisUavTable.getTime());
        return toAjax(basisUavTableService.insertBasisUavTable(basisUavTable));
    }

    /**
     * 修改目标的参数
     */
    @Log(title = "目标的参数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasisUavTable basisUavTable)
    {
        basisUavTable.setTimeslot(basisUavTable.getTime());
        return toAjax(basisUavTableService.updateBasisUavTable(basisUavTable));
    }

    /**
     * 删除目标的参数
     */
    @Log(title = "目标的参数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(basisUavTableService.deleteBasisUavTableByIds(ids));
    }


    @GetMapping(value = "/findByLoadingIdAndUavIdfFormUavTable")
    public AjaxResult findByLoadingIdAndUavIdfFormUavTable(String loadingId,Integer uavId)
    {
        return success(basisUavTableService.findByLoadingIdAndUavIdfFormUavTable(loadingId,uavId));
    }


    @GetMapping(value = "/findByLoadingIdAndUavIdfFormUavTableDT")
    public AjaxResult findByLoadingIdAndUavIdfFormUavTableDT(String loadingId,Integer uavId)
    {
        return success(basisUavTableService.findByLoadingIdAndUavIdfFormUavTableDT(loadingId,uavId));
    }



    @PostMapping("/uploadUav")
    public String uploadFile(@RequestParam("file") MultipartFile file){
        Map<String, Object> map = new HashMap<>();
        String fileName = "";

        if (file.isEmpty()) {
            return "上传失败：文件为空";
        }

        try {
            Path uploadPath = Paths.get(uploadFilePath);
            if (!Files.exists(uploadPath)) {
                Files.createDirectories(uploadPath);
            }

            // 生成唯一文件名，防止冲突和路径遍历
            String originalFileName = StringUtils.cleanPath(file.getOriginalFilename());
            fileName = UUID.randomUUID().toString() + "_" + originalFileName;

            // 保存文件到指定路径
            Path filePath = uploadPath.resolve(fileName);
            Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
            return uploadFilePath+"/"+fileName;

        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败";
        }

    }


    @GetMapping(value = "/findByLoadingIdfFormUavTable")
    public AjaxResult findByLoadingIdfFormUavTable(String loadingId)
    {
        return success(basisUavTableService.findByLoadingIdfFormUavTable(loadingId));
    }

    @GetMapping(value = "/findByLoadingIdfAndBusinessInformationFormUavTable")
    public AjaxResult findByLoadingIdfAndBusinessInformationFormUavTable(String loadingId)
    {
        return success(basisUavTableService.findByLoadingIdfAndBusinessInformationFormUavTable(loadingId));
    }
}
